package com.company.project.controller.ldjsc.weather;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.company.project.common.utils.DateUtils;
import com.company.project.entity.ldjsc.home.CockpitContentEntity;
import com.company.project.entity.ldjsc.home.JscJcbgXqEntity;
import com.company.project.entity.ldjsc.weather.ZyCityCodeEntity;
import com.company.project.entity.ldjsc.weather.ZyMeteorologicalWarningEntity;
import com.company.project.entity.ldjsc.weather.ZyWeatherSevenEntity;
import com.company.project.service.ldjsc.home.CockpitContentService;
import com.company.project.service.ldjsc.home.JscJcbgXqService;
import com.company.project.service.ldjsc.weather.ZyCityCodeService;
import com.company.project.service.ldjsc.weather.ZyMeteorologicalWarningService;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.company.project.common.utils.DataResult;


/**
 * 气象预警信息表
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2025-09-11 11:41:02
 */
@Controller
@RequestMapping("/sxzy/")
public class ZyMeteorologicalWarningController {
    @Autowired
    private ZyMeteorologicalWarningService zyMeteorologicalWarningService;

    @Autowired
    private ZyCityCodeService zyCityCodeService;

    @Autowired
    private CockpitContentService cockpitContentService;

    @Autowired
    private JscJcbgXqService jscJcbgXqService;

    @ApiOperation(value = "查询分页数据")
    @GetMapping("zyMeteorologicalWarning/listByPage")
    @ResponseBody
    public DataResult findListByPage(ZyMeteorologicalWarningEntity zyMeteorologicalWarning) {

        String color = "";
        if (StringUtils.isNotBlank(zyMeteorologicalWarning.getColor())) {
            if (zyMeteorologicalWarning.getColor().equals("蓝色预警")) {
                color = "Blue";
            } else if (zyMeteorologicalWarning.getColor().equals("黄色预警")) {
                color = "Yellow";
            } else if (zyMeteorologicalWarning.getColor().equals("红色预警")) {
                color = "Red";
            } else if (zyMeteorologicalWarning.getColor().equals("橙色预警")) {
                color = "Orange";
            }
        }

        //获取当前时间的前24小时
        LocalDateTime now = LocalDateTime.now().minusHours(24);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(formatter);

        if (StringUtils.isNotBlank(zyMeteorologicalWarning.getWarningArea())){
            //查询市及下属区
            if (zyMeteorologicalWarning.getWarningArea().equals("太原")){
                //查询市下面的城市列表(太原)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "1");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("太原");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                //查询条件示例
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("大同")){
                //查询市下面的城市列表(大同)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "12");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("大同");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("朔州")){
                //查询市下面的城市列表(朔州)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "23");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("朔州");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("忻州")){
                //查询市下面的城市列表(忻州)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "30");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("忻州");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("吕梁")){
                //查询市下面的城市列表(吕梁)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "46");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("吕梁");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("晋中")){
                //查询市下面的城市列表(晋中)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "59");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("晋中");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("阳泉")){
                //查询市下面的城市列表(阳泉)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "71");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("阳泉");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("长治")){
                //查询市下面的城市列表()
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "77");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("长治");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("晋城")){
                //查询市下面的城市列表(晋城)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "90");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("晋城");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("临汾")){
                //查询市下面的城市列表(临汾)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "97");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("临汾");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else if (zyMeteorologicalWarning.getWarningArea().equals("运城")){
                //查询市下面的城市列表(运城)
                LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyCityCodeEntity::getSuperiorId, "115");
                List<ZyCityCodeEntity> list = zyCityCodeService.list(lqw);
                List<String> cityNames = new ArrayList<>();
                for (ZyCityCodeEntity zyCityCodeEntity : list) {
                    cityNames.add(zyCityCodeEntity.getCityName());
                }
                cityNames.add("运城");
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ZyMeteorologicalWarningEntity::getWarningArea, cityNames);
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            } else {
                //查询县级
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                //查询条件示例
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getWarningArea()), ZyMeteorologicalWarningEntity::getWarningArea, zyMeteorologicalWarning.getWarningArea());
                queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
                queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
                queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
                IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
                return DataResult.success(iPage);
            }
        } else {
            //查询全省
            LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
            //查询条件示例
            queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getWarningArea()), ZyMeteorologicalWarningEntity::getWarningArea, zyMeteorologicalWarning.getWarningArea());
            queryWrapper.eq(StringUtils.isNotBlank(zyMeteorologicalWarning.getColor()), ZyMeteorologicalWarningEntity::getColor, color);
            queryWrapper.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
            queryWrapper.orderByDesc(ZyMeteorologicalWarningEntity::getSendTime);
            IPage<ZyMeteorologicalWarningEntity> iPage = zyMeteorologicalWarningService.page(zyMeteorologicalWarning.getQueryPage(), queryWrapper);
            return DataResult.success(iPage);
        }
    }

    @ApiOperation(value = "查看详情")
    @GetMapping("zyMeteorologicalWarning/{id}")
    @ResponseBody
    public DataResult findOne(@PathVariable String id) {
        return DataResult.success(zyMeteorologicalWarningService.getById(id));
    }

    //获取全省的市
    @GetMapping("zyMeteorologicalWarning/city")
    @ResponseBody
    public DataResult city() {
        //获取市级列表
        LambdaQueryWrapper<ZyCityCodeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZyCityCodeEntity::getSuperiorId, "0");
        List<ZyCityCodeEntity> list = zyCityCodeService.list(queryWrapper);
        //获取每个市的县
        for (ZyCityCodeEntity zyCityCodeEntity : list) {
            LambdaQueryWrapper<ZyCityCodeEntity> lqw = Wrappers.lambdaQuery();
            lqw.eq(ZyCityCodeEntity::getSuperiorId, zyCityCodeEntity.getId());
            List<ZyCityCodeEntity> zyCityCodeEntities = zyCityCodeService.list(lqw);
            zyCityCodeEntity.setChildren(zyCityCodeEntities);
        }
        return DataResult.success(list);
    }

    //获取全市的县
    @GetMapping("zyMeteorologicalWarning/county")
    @ResponseBody
    public DataResult county(String city) {
        LambdaQueryWrapper<ZyCityCodeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZyCityCodeEntity::getSuperiorId, city);
        List<ZyCityCodeEntity> list = zyCityCodeService.list(queryWrapper);
        return DataResult.success(list);
    }

    //获取全省气象预警（测试无需停止）
    @Scheduled(cron = "0 0/10 * * * ?")
    private void getWarnInfoByLocation() {
//    @GetMapping("zyMeteorologicalWarning/add")
//     public void add(){
        try {
            // 创建URL对象
            URL url = new URL("http://api.sxqx.gov.cn/getWarnInfoByLocation.aspx");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");

            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Java HTTP Client)");

            // 启用输出（关键：这会确保Content-Length被正确设置）
            connection.setDoOutput(true);

            // 设置其他参数
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(30000);

            // 发送空的POST数据（Content-Length将被设置为0）
            try (OutputStream os = connection.getOutputStream()) {
                // 不写入任何数据，但保持输出流打开和关闭
                // 这样会自动设置Content-Length: 0
            }

            // 检查响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应
            BufferedReader reader;
            if (responseCode >= 200 && responseCode < 300) {
                reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), "UTF-8")
                );
            } else {
                reader = new BufferedReader(
                        new InputStreamReader(connection.getErrorStream(), "UTF-8")
                );
            }

            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line).append("\n");
            }

            reader.close();
            connection.disconnect();


            // 解析 JSON 数据
            JSONObject result = JSONObject.parseObject(response.toString());
            JSONArray futureArray = result.getJSONArray("data");

            System.out.println("气象预警拉取时间" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            // 构建返回数据
            for (int j = 0; j < futureArray.size(); j++) {
                JSONObject day = futureArray.getJSONObject(j);
                ZyMeteorologicalWarningEntity zyMeteorologicalWarningEntity = new ZyMeteorologicalWarningEntity();

                String headline = day.getString("headline");

                zyMeteorologicalWarningEntity.setHeadline(headline);

                //截取标题前面的城市
                System.out.println("标题: " + headline);
                String substring = headline.substring(0, 5);
                System.out.println("截取的标题: " + substring);
                String warningArea = "";
                if (substring.contains("省")) {
                    int countyIndex = headline.indexOf("省");
                    warningArea = headline.substring(0, countyIndex);
                } else if (substring.contains("市")) {
                    int countyIndex = headline.indexOf("市");
                    warningArea = headline.substring(0, countyIndex);
                } else if (substring.contains("县")) {
                    int countyIndex = headline.indexOf("县");
                    warningArea = headline.substring(0, countyIndex);
                    if (countyIndex == 1){
                        warningArea = warningArea + "县";
                    }

                } else if (substring.contains("区")) {
                    int countyIndex = headline.indexOf("区");
                    warningArea = headline.substring(0, countyIndex);
                    if (warningArea.equals("尖草坪")){
                        warningArea = "尖草坪区";
                    } else if (warningArea.equals("小店")){
                        warningArea = "小店区";
                    }

                }
                System.out.println("预警地区: " + warningArea);
                zyMeteorologicalWarningEntity.setWarningArea(warningArea);
                zyMeteorologicalWarningEntity.setColor(day.getString("color"));
                zyMeteorologicalWarningEntity.setSendTime(day.getString("sendTime"));

                String eventType = "";

                if (headline.contains("暴雨")){
                    eventType = "暴雨";
                } else if (headline.contains("寒潮")){
                    eventType = "寒潮";
                } else if (headline.contains("霜冻")){
                    eventType = "霜冻";
                } else if (headline.contains("暴雪")){
                    eventType = "暴雪";
                } else if (headline.contains("沙尘暴")){
                    eventType = "沙尘暴";
                } else if (headline.contains("高温")){
                    eventType = "高温";
                } else if (headline.contains("冰雹")){
                    eventType = "冰雹";
                } else if (headline.contains("森林火险")){
                    eventType = "森林火险";
                } else if (headline.contains("道路结冰")){
                    eventType = "道路结冰";
                } else if (headline.contains("雷电")){
                    eventType = "雷电";
                } else if (headline.contains("山体滑坡")){
                    eventType = "山体滑坡";
                } else if (headline.contains("大风")){
                    eventType = "大风";
                } else if (headline.contains("强季风")){
                    eventType = "强季风";
                } else if (headline.contains("干旱")){
                    eventType = "干旱";
                } else if (headline.contains("霾")){
                    eventType = "霾";
                } else if (headline.contains("大雾")){
                    eventType = "大雾";
                } else if (headline.contains("雷暴")){
                    eventType = "雷暴";
                } else if (headline.contains("雷雨")){
                    eventType = "雷雨";
                } else if (headline.contains("低温")){
                    eventType = "低温";
                } else if (headline.contains("强对流")){
                    eventType = "强对流";
                } else if (headline.contains("冰冻")){
                    eventType = "冰冻";
                } else if (headline.contains("解除")){
                    eventType = "解除";
                } else {
                    eventType = "默认";
                }

                zyMeteorologicalWarningEntity.setEventType(eventType);//风险类型

                zyMeteorologicalWarningEntity.setInfo(day.getString("info"));
                zyMeteorologicalWarningEntity.setMsgType(day.getString("msgType"));
                zyMeteorologicalWarningEntity.setNote(day.getString("note"));

                //判断数据是否已经存在
                LambdaQueryWrapper<ZyMeteorologicalWarningEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(ZyMeteorologicalWarningEntity::getSendTime, zyMeteorologicalWarningEntity.getSendTime());
                queryWrapper.eq(ZyMeteorologicalWarningEntity::getHeadline, zyMeteorologicalWarningEntity.getHeadline());
                queryWrapper.eq(ZyMeteorologicalWarningEntity::getInfo, zyMeteorologicalWarningEntity.getInfo());
                List<ZyMeteorologicalWarningEntity> list = zyMeteorologicalWarningService.list(queryWrapper);
                if (list == null || list.size() == 0) {
                    System.out.println("插入的数据内容为：" + zyMeteorologicalWarningEntity);
                    zyMeteorologicalWarningService.save(zyMeteorologicalWarningEntity);

                    System.out.println("日期：" + day.getString("sendTime"));
                    System.out.println("标题：" + day.getString("headline"));
                }
            }

            //先删除监测报告中气象预警的数据
            LambdaQueryWrapper<JscJcbgXqEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(JscJcbgXqEntity::getSource_from, "气象预警");
            jscJcbgXqService.remove(queryWrapper);

            //查询获取气象表的近24小时数据
            LocalDateTime now = LocalDateTime.now().minusHours(24);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDate = now.format(formatter);

            LambdaQueryWrapper<ZyMeteorologicalWarningEntity> lqw = Wrappers.lambdaQuery();
            lqw.gt(ZyMeteorologicalWarningEntity::getSendTime, formattedDate);
            List<ZyMeteorologicalWarningEntity> list = zyMeteorologicalWarningService.list(lqw);

            //插入到监测报告表中
            for (ZyMeteorologicalWarningEntity zyMeteorologicalWarningEntity : list) {
                JscJcbgXqEntity JscJcbgXqEntity = new JscJcbgXqEntity();
                JscJcbgXqEntity.setId(zyMeteorologicalWarningEntity.getId().toString());//id
                JscJcbgXqEntity.setTitle(zyMeteorologicalWarningEntity.getHeadline());//标题
                JscJcbgXqEntity.setContent_url(zyMeteorologicalWarningEntity.getInfo());//内容
                JscJcbgXqEntity.setSource_from("气象预警");//来源
                JscJcbgXqEntity.setPrefix_title(zyMeteorologicalWarningEntity.getEventType());//风险类型
                JscJcbgXqEntity.setCreate_time(zyMeteorologicalWarningEntity.getSendTime());//创建时间
                JscJcbgXqEntity.setIs_new(zyMeteorologicalWarningEntity.getColor());
                jscJcbgXqService.save(JscJcbgXqEntity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
